##########################################################################
#
#  Copyright (c) 2012-2014, Image Engine Design Inc. All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#      * Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      * Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials provided with
#        the distribution.
#
#      * Neither the name of John Haddon nor the names of
#        any other contributors to this software may be used to endorse or
#        promote products derived from this software without specific prior
#        written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
#  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################

import imath

import IECore

import Gaffer
import GafferUI

class MenuButton( GafferUI.Button ) :

	def __init__( self, text="", image=None, hasFrame=True, menu=None, immediate=False, **kw ) :

		GafferUI.Button.__init__( self, text, image, hasFrame, **kw )

		self.__menu = None
		self.setMenu( menu )
		self.setImmediate( immediate )

		self.buttonPressSignal().connect( Gaffer.WeakMethod( self.__buttonPress ) )
		self.clickedSignal().connect( Gaffer.WeakMethod( self.__clicked ) )

	def setMenu( self, menu ) :

		# Note that although QPushButton does have a setMenu() method that would
		# ostensibly do everything for us, we don't use it. Primarily this is because
		# it pops the menu up in utterly the wrong position when we place a MenuButton
		# in GLWidget overlay - this is a Qt bug. Secondarily, we also want to use
		# Menu.popup() as it gives the menu a parent for return from Menu.parent(),
		# which may be important to the menu item callbacks.

		if menu is self.__menu :
			return

		self.__menu = menu

		if self.__menu is not None :
			self.__menuVisibilityChangedConnection = self.__menu.visibilityChangedSignal().connect(
				Gaffer.WeakMethod( self.__menuVisibilityChanged ),
				scoped = True
			)
		else :
			self.__menuVisibilityChangedConnection = None

		self.setEnabled( self.__menu is not None )

	def getMenu( self ) :

		return self.__menu

	def setText( self, text ) :

		GafferUI.Button.setText( self, text )

		# Because we can't use QPushButton::setMenu() to manage our menus,
		# we also can't use the QPushButton::menu-indicator subcontrol to
		# style menus. Instead we use this custom property to drive the
		# stylesheet.
		wantMenuIndicator = bool( text )
		haveMenuIndicator = self._qtWidget().property( "gafferMenuIndicator" ) or False
		if wantMenuIndicator != haveMenuIndicator :
			self._qtWidget().setProperty( "gafferMenuIndicator", wantMenuIndicator )
			self._repolish()

	## If the button is immediate and the menu contains only a single
	# item, then that item will be executed directly when the button is pressed,
	# instead of showing the menu.
	def setImmediate( self, immediate ) :

		self.__immediate = immediate

	def getImmediate( self ) :

		return self.__immediate

	def setErrored( self, errored ) :

		if errored == self.getErrored() :
			return

		self._qtWidget().setProperty( "gafferError", GafferUI._Variant.toVariant( bool( errored ) ) )
		self._repolish()

	def getErrored( self ) :

		return GafferUI._Variant.fromVariant( self._qtWidget().property( "gafferError" ) ) or False

	def __buttonPress( self, widget, event ) :

		# We show the menu on `buttonPress` rather than `clicked` as it
		# allows the user to keep the button held, move over an item and
		# then release to trigger it - see #742.

		if event.button != event.Buttons.Left or self.__menu is None :
			return False

		if self.getImmediate() :
			# Build the top level menu so we can query its contents.
			# We don't call `Menu._buildFully()` as that also builds
			# submenus, which can be costly.
			self.__menu._qtWidget().aboutToShow.emit()
			if len( self.__menu._qtWidget().actions() ) == 1 :
				# Only one item. Do nothing now, so we can trigger
				# it directly in `__clicked()` (like a regular button
				# press, which is enacted on release).
				return False

		b = self.bound()
		self.__menu.popup(
			parent = self,
			position = imath.V2i( b.min().x, b.max().y ),
		)

		return True

	def __clicked( self, widget ) :

		if self.getImmediate() :
			actions = self.__menu._qtWidget().actions()
			if len( actions ) == 1 :
				actions[0].trigger()
				return True

		return False

	def __menuVisibilityChanged( self, menu ) :

		if not menu.visible() :
			self._qtWidget().setDown( False )
			# There is a bug whereby Button never receives the event for __leave,
			# if the menu is shown. This results in the image highlight state sticking.
			if self.widgetAt( self.mousePosition() ) is not self :
				self._Button__leave( self )
